<?php
/**
*A simple collection of pages with a streamlined linear index.
*<p>Each page has its own box.</p>
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@package Books
*@subpackage Simple
*/
class BookSimple extends PageBook
{
	/**
	* Build the book. A "default_page" is set.
	*@param string $title book title
	*/
	public function __construct( $title )
	{
		parent::__construct( $title, "inline" );
		$this->SetStorage( array("default_page" => false ) );
	}
	/**
	*Build the required HTML.
	*@param string $page The page id
	*/
	protected function SheetGenerateHtml( $page )
	{
		$this->AddControl( "SimpleBookManage", I18N("Configure pages") );
		$this->AddControl( "SimpleBookAdd", I18N("Create new page") );

		$page_id = $this->GetChild( $page );
		if ( !$page_id )
			$page_id = $this->Get("default_page");
		return Box::Draw( $page_id );
	}
}
/**
*Configure a simple collection of pages with a streamlined linear index.
*<p>Each page has its own box.</p>
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@package Books
*@subpackage Simple
*/
class SimpleBookManage extends BoxEdit
{
	protected function BoxEdit( $box )
	{
		$html = new HtmlEditRequest();

		$html->Append( $fs = new HtmlFieldset(I18N("Sort pages") ) );
		$list = $box->ListPages();
		$def = $box->Get("default_page");
		$def_title = "";
		$opts = array();
		foreach ( $list as $id => $data )
		{ # note: do not use $p as index in select because it could not be unique!
			$item = AtomCache::GetAtomData( $id );
			$atom = AtomCache::GetAtom( $id );
			$type = get_class( $item );
			$tags = $atom->GetTagCloud();
			$tags = "(".implode(",", $tags).")";
			$opts[$id] = "[$type] ".$data["title"]." $tags";
			if ( $id == $def )
				$def_title = $data["title"];
		}
		$fs->Append( $s = new HtmlSpecialSelect("new_sort", $opts) );
		$sid = $s->GetId();


		$html->Append( $fs = new HtmlFieldset(I18N("Default page") ) );
		$fid = $fs->GetId();

		$click = "var d=\$('#$sid select option:selected');
				alert(d)
				\$('#$fid > input').val(d.val())
				\$('#$fid > span').html( d.attr('text'))
				return false";
		$fs->Append( new HtmlButton(I18N("Set as default page..."), $click) );

		$fs->Append( new HtmlHiddenInput("default_page", $def ) );
		$fs->Append( $sp =new HtmlTag("span") );
		$sp->Append( $def_title );
		$html->AppendSubmit( I18N("Save and confirm") );
		return $html;
	}
	protected function BoxSave( $box )
	{
		$new_list = Core::PostVar("new_sort", false, "serialized");
		$default = Core::PostVar("default_page", false, "string");
		if ( $new_list !== false )
		{
			$cur_list = $box->ListPages();

			foreach ( $cur_list as $id => $dummy )
				$box->RemovePage( $id );

			foreach ( $new_list as $id => $dummy )
				if ( isset( $cur_list[$id] ) )
					$box->AddPage( $id,  $cur_list[ $id ] );
			$this->SetReload(true);
		}
		$box->Set("default_page", $default) && $this->SetReload();
		return I18N("Configuredf saved");
	}
}
/**
*Add a page to e collection of pages with a streamlined linear index.
*<p>Each page has its own box.</p>
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@package Books
*@subpackage Simple
*/
class SimpleBookAdd extends BoxEdit
{
	protected function BoxEdit( $box )
	{
		$atom = $box->GetAtom();
		$html = new HtmlEditRequest();
		$html->Append( $fs = new HtmlFieldset(I18N("Add a page to this book") ) );
		$tags = array_merge( array("page"), $atom->GetTagCloud());
		$fs->Append( new BoxCollector( "new_box", array("category" => "box",
													"type" => "all",
													"tags" => $tags)  ) );
		$html->AppendSubmit(I18N("Select box"));
		return $html;
	}
	protected function BoxSave( $box )
	{
		$new_box = Core::PostVar("new_box", false, "string");
		if ( $new_box != false )
		{
			$nbox = AtomCache::GetAtomData( $new_box );
			$new_id = $box->AddPage( $new_box, array("title" => $nbox->GetTitle()) );
			$this->SetReload(true);
			return I18N("Page addedd successfully!")." ($new_id)";
		}
		return I18N("Please specify a box");
	}
}
Box::$description["BookSimple"] = "Simple book (list of pages)";
?>